﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Reflection;

using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Communications;

using CharityGamesOnline.Modules.Common;

namespace CharityGamesOnline.Modules.Charity
{
    public partial class CharityDetail : CGOBase
    {
        public const int IMAGE_MAX_WIDTH = 119;
        public const int IMAGE_MAX_HEIGHT = 113;
        protected int charityID = 0;

        public int CharityID
        {
            get
            {
                if (charityID == 0)
                {
                    if ((Request.QueryString["CharityID"] != null))
                    {
                        charityID = Int32.Parse(Request.QueryString["CharityID"]);
                    }
                }
                return charityID;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {                
                // If this is the first visit to the page, bind the role data to the datalist 
                if (Page.IsPostBack == false)
                {
                    BindData();
                }
            }

            catch (Exception exc)
            {
                //Module failed to load 
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #region Public Function

        public void BindData(int intCharityID)
        {
            charityID = intCharityID;
            BindData();
        }

        #endregion

        #region Private Function

        private void BindData()
        {
            if (CharityID>0)
            {
                // get content 
                CharityController ctrlCharity = new CharityController();
                CharityInfo objCharity = ctrlCharity.GetCharity(PortalId, CharityID);

                if ((objCharity != null))
                {
                    BindCharity(objCharity);
                }
            }
        }

        private void BindCharity(CharityInfo objCharity)
        {
            lblName.Text = objCharity.Name;
            litDescription.Text = Server.HtmlDecode(objCharity.Description);            
            if (string.IsNullOrEmpty(objCharity.WebSite))
            {
                lnkCharity.HRef = string.Empty;
                lnkDonate.HRef = string.Empty;
            }
            else
            {
                lnkCharity.HRef = objCharity.WebSite;
                lnkDonate.HRef = objCharity.WebSite;
            }            

            imgLogo.Visible = false;
            if (objCharity.ImageDisplay && !string.IsNullOrEmpty(objCharity.ImageURL))
            {
                int intWidth = objCharity.ImageWidth;
                int intHeight = objCharity.ImageHeight;
                imgLogo.Visible = true;
                imgLogo.ImageUrl = GetIconUrl(objCharity, "ImageURL");
                
                if (intWidth > IMAGE_MAX_WIDTH)
                {
                    intWidth = IMAGE_MAX_WIDTH;
                    intHeight = intHeight * IMAGE_MAX_WIDTH / intWidth;
                }

                if (intHeight > IMAGE_MAX_HEIGHT)
                {
                    intHeight = IMAGE_MAX_HEIGHT;
                    intWidth = intWidth * IMAGE_MAX_HEIGHT / intHeight;
                }

                imgLogo.Width = intWidth;
                imgLogo.Height = intHeight;               
            }
        }

        #endregion
    }
}